/**
 * Created with yyl.
 * Date: 2018/11/21 0027
 * Time: 17:26
 */

export default class getTextByValue{
  static getTextByValue = function (value, itemList) {
      var item = itemList.find(function(item){
          return item.get('value') == value
      });
      if(item){
          return item.get('text')
      }
      return "";
  }

  static getNameById = function (id, itemList) {
      var item = itemList.find(function(item){
          return item.get('id') == id
      });
      if(item){
          return item.get('name')
      }
      return "";
  }

    static getNameByEmpId = function (userId, itemList) {
        var item = itemList.find(function(item){
            return item.get('userId') == userId
        });
        if(item){
            return item.get('name')
        }
        return "";
    }

    static getNameByAuntId = function (auntId, itemList) {
        var item = itemList.find(function(item){
            return item.get('auntId') == auntId
        });
        if(item){
            return item.get('name')
        }
        return "";
    }

    static getNameByComboNo = function (comboNo, itemList) {
        var item = itemList.find(function(item){
            return item.get('comboNo') == comboNo
        });
        if(item){
            return item.get('name')
        }
        return "";
    }

    static getNameListByItemId = function (id, tradeItemList,childItemVoList) {
        let tradeItem = tradeItemList.filter(function(item){
            return item.get('refId') == id
        });
        let list = [];
        if(tradeItem && tradeItem.size > 0){
            tradeItem.map(it=>{
                var childItem = childItemVoList.find(item=>{
                    return it.get('itemId') == item.get('id')
                });
                if(childItem){
                    if(!it.get('itemName')){
                        return "";
                    }
                    let billNo = it.get('billNo');
                    if(billNo){
                        childItem = childItem.set('billNo', billNo);
                    }
                    let refId = childItem.get("refId");
                    if(!refId){
                        childItem = childItem.set('refId', id);
                    }
                    let duserItemPrice = childItem.get("duserItemPrice");
                    if (!duserItemPrice) {
                        childItem = childItem.set('duserItemPrice', 0);
                    }
                    let itemId = childItem.get("itemId");
                    if(!itemId){
                        childItem = childItem.set('itemId', childItem.get("id"));
                    }
                    let salePrice = childItem.get('salePrice');
                    if (!salePrice) {
                        childItem = childItem.set('salePrice', childItem.get("duserItemPrice"));
                    }
                    let itemName = childItem.get('itemName');
                    if (!itemName) {
                        childItem = childItem.set('itemName', childItem.get('name'));
                    }
                    let param = {};
                    param.key = childItem;
                    let value = childItem.get('name');
                    if(value && value.length > 10){
                        value = value.substr(0, 10).concat('…');
                    }
                    param.label = value;
                    list.push(param)
                }
            })
            return list;
        }
        return "";
    }


    static getNameByItemId = function (id, tradeItemList,childItemVoList) {
        var tradeItem = tradeItemList.find(function(item){
            return item.get('refId') == id
        });
        if(tradeItem){
            var childItem = childItemVoList.find(item=>{
                return tradeItem.get('itemId') == item.get('id')
            });
            if(childItem){
                if(!tradeItem.get('itemName')){
                    return "";
                }
                return childItem.get('name')
            }
        }
        return "";
    }

}


